<?php
namespace app\api\controller\v1;
class Userbank extends Base
{
	//列表
    public function lists()
    {
    	//数据验证
    	$d = request()->post();
    	$rule = [
            'page'=>'require|integer',
            'limit'=>'require|integer',
        ];
        $msg = [];
        $r = $this->validate($d, $rule,$msg);
        if($r!==true) return err($r);


        //操作模型
        $model = model('userBank');
    	$map = [
    		[ 'user_id', '=', $this->userinfo['id'] ]
    	];
    	$lists = $model->where($map)
    				->order('id desc')
    				->paginate($d['limit'],true,[ 'page' => $d['page'] ]);
    	//返回数据
       	$rs = $lists->visible( ["id","bank_name","bank_no","is_default"] )->toArray();
       	if($rs)
       		return suc($rs['data'], $rs['total']);	
        return suc([],0);
    }

    //添加
    public function add()
    {
    	$d = request()->post();
    	$d['user_id'] = $this->userinfo['id'];

    	if( model('userBank')->renew($d) )
			return suc();

       	return err( model('userBank')->getError() );
    }


    //删除
    public function delete()
    {
    	//数据验证
    	$d = request()->post();
    	$rule = [
            'id'=>'require|integer',
        ];
        $msg = [];
        $r = $this->validate($d, $rule,$msg);
        if($r!==true) return err($r);

    	$d = request()->post();

    	model('userBank')::destroy($d['id'],true);

		return suc();
    }

    //设为默认
    public function default()
    {
    	//数据验证
    	$d = request()->post();
    	$rule = [
            'id'=>'require|integer',
        ];
        $msg = [];
        $r = $this->validate($d, $rule,$msg);
        if($r!==true) return err($r);

    	$d = request()->post();

    	model('userBank')->default($this->userinfo['id'] , $d['id']);

		return suc();
    }
}
